package cn.sciento.iam.saas.config;

import java.util.List;
import cn.sciento.iam.config.IamProperties;
import cn.sciento.iam.saas.domain.service.TenantCoreService;
import cn.sciento.iam.saas.domain.service.TenantEventBus;
import cn.sciento.iam.saas.domain.service.TenantObserver;
import cn.sciento.iam.saas.domain.service.impl.TenantCoreServiceImpl;
import cn.sciento.iam.saas.domain.service.observer.TenantConfigObserver;
import cn.sciento.iam.saas.domain.service.observer.TenantGroupObserver;
import cn.sciento.iam.saas.domain.service.observer.TenantLdapObserver;
import cn.sciento.iam.saas.domain.service.observer.TenantPasswordPolicyObserver;
import cn.sciento.iam.saas.domain.service.observer.TenantRoleObserver;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableConfigurationProperties({IamProperties.class})
@Configuration
public class TenantConfiguration {
  @Bean
  @ConditionalOnMissingBean({TenantConfigObserver.class})
  public TenantConfigObserver tenantConfigObserver() {
    return new TenantConfigObserver();
  }
  
  @Bean
  @ConditionalOnMissingBean({TenantGroupObserver.class})
  public TenantGroupObserver groupTenantObserver() {
    return new TenantGroupObserver();
  }
  
  @Bean
  @ConditionalOnMissingBean({TenantLdapObserver.class})
  public TenantLdapObserver ldapTenantObserver() {
    return new TenantLdapObserver();
  }
  
  @Bean
  @ConditionalOnMissingBean({TenantPasswordPolicyObserver.class})
  public TenantPasswordPolicyObserver passwordPolicyTenantObserver() {
    return new TenantPasswordPolicyObserver();
  }
  
  @Bean
  @ConditionalOnMissingBean({TenantRoleObserver.class})
  public TenantRoleObserver roleTenantObserver() {
    return new TenantRoleObserver();
  }
  
  @Bean
  @ConditionalOnMissingBean({TenantEventBus.class})
  public TenantEventBus tenantEventBus(List<TenantObserver<?>> tenantObservers) {
    return new TenantEventBus(tenantObservers);
  }
  
  @Bean
  @ConditionalOnMissingBean({TenantCoreService.class})
  public TenantCoreService tenantCoreService() {
    return (TenantCoreService)new TenantCoreServiceImpl();
  }
}
